package UI;

import BLL.BookService;
import BLL.BorrowBookService;
import BLL.ReaderService;
import BLL.ReaderTypeService;
import DAL.DepositSetting;
import Entity.Books;
import Entity.Borrow;
import Entity.Category;
import Entity.Reads;
import Utils.TSUtility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @Author Chen
 * @Date 2022/1/6 17:42
 * @Version 1.0
 * 图书借阅管理界面
 */
public class BookBorrowingUI {
    public static void main(String[] args) throws ParseException {
        BookBorrowingUI.ui();
    }
    public static void ui() throws ParseException {
        Scanner sc=new Scanner(System.in);
        boolean option=true;
        while (option){
            System.out.println("************图书管理系统***********");
            System.out.println("************图书借阅管理***********");
            System.out.println("1.图书借阅");
            System.out.println("2.图书归还");
            System.out.println("3.图书借阅情况！");
            System.out.println("4.退出");
            System.out.println("************图书借阅管理***********");
            System.out.println("************图书借阅系统***********");
            System.out.println("请选择：");
            String number=sc.next();
            switch (number){
                case "1":
                    System.out.println("************图书管理系统***********");
                    System.out.println("************图书借阅管理***********");
                    System.out.println("请输入图书编号：");
                    int bookID= TSUtility.readInt();
                    Books books = BookService.selectBookById(bookID);
                    if (books==null){
                        System.out.println("没有图书编号为"+bookID+"的信息！");
                        break;
                    }
                    System.out.println("请输入读者编号：");
                    int readID=TSUtility.readInt();
                    Reads reads1 = ReaderService.selectReaderById(readID);
                    if (reads1==null) {
                        System.out.println("没有读者编号为" + readID + "的信息！");
                        break;
                    }
                    //获取当前的时间，用于在数据库中保存记录
                    Date borrowTime=new Date();
                    int i = BorrowBookService.borrowBook(bookID, readID,borrowTime);
                    System.out.println(i+"记录添加！");
                    System.out.println("************图书借阅管理***********");
                    System.out.println("************图书管理系统***********");
                    break;
                case "2":
                    System.out.println("************图书管理系统***********");
                    System.out.println("************图书归还管理***********");
                    System.out.println("请输入图书编号：");
                    int bookID1=sc.nextInt();
                    Borrow borrows = BorrowBookService.seeBook(bookID1);
                    if (borrows==null){
                        System.out.println("没有编号为"+bookID1+"的图书信息");
                        break;
                    }
                    int readID1=borrows.getReadersNumber();
                    Reads reads = ReaderService.selectReaderById(readID1);
                    int readesTypeNumber = reads.getReadesTypeNumber();
                    Category category = ReaderTypeService.selectReaderTypeNumber(readesTypeNumber);
                    int days = category.getDays();
                    double fine = category.getFine();
                    int days1 = category.getDays();
                    int day=category.getDays();
                    //归还日期
                    Date d2 = new Date();
                    Date d1=borrows.getHoursOfLoanService();
                    long secend=(d2.getTime()-d1.getTime())/1000/60/60/24;
                    int day1=(int) secend-day;
                    double f1=0.0;
                    if (day1>0){
                        if (days1<day1) {
                            //罚金
                            f1 = (double) day1 * fine;
                            if (f1 > 0.0) {
                                DepositSetting ds = new DepositSetting();
                                ds.addFile(bookID1, readID1, f1);
                            }
                        }
                    }
                    int readersNumber = borrows.getReadersNumber();
                    int i1 = BorrowBookService.returnBook(readID1, d1, d2, f1, bookID1);
                    System.out.println(i1+"记录被修改！");
                    System.out.println("************图书归还管理***********");
                    System.out.println("************图书管理系统***********");
                    break;
                case "3":
                    System.out.println("************图书管理系统***********");
                    System.out.println("************图书借阅管理***********");
                    List<Borrow> borrows1 = BorrowBookService.lookBook();
                    if (borrows1.size()==0){
                        System.out.println("没有借阅信息！");
                        break;
                    }
                    System.out.println("图书编号     读者编号      借阅日期          归还日期        罚金");
                    borrows1.forEach(System.out::println);
                    System.out.println("************图书借阅管理***********");
                    System.out.println("************图书管理系统***********");
                    break;
                case "4": option=false;break;
                default:
                    System.out.println("没有"+number+"这个选项！");break;
            }
        }
    }
}
